package es.cea.agendatelefonica;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GuardarNuevoContactoServlet
 */
@WebServlet("/guardar")
public class GuardarNuevoContactoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GuardarNuevoContactoServlet() {
      
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List<ContactoTel>contactos= (List)request.getSession().getServletContext().getAttribute(AgendaAtributos.contactos.toString());
	
		ContactoTel contactoNuevo = new ContactoTel();
		int tamanyo = contactos.size();
		contactoNuevo.setId(contactos.get(tamanyo-1).getId()+1);
		contactoNuevo.setNombre(request.getParameter(AgendaAtributos.nombre.toString()));
		contactoNuevo.setTelefono(request.getParameter(AgendaAtributos.telefono.toString()));
		
		contactos.add(contactoNuevo);
		request.getRequestDispatcher("nuevoContactoGuardado.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
